﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using JinRi.Services;
using JinRi.Services.Domains;
using JinRiAdmin.WebHost.ViewModels;
using PagedList;

namespace JinRiAdmin.WebHost.Controllers
{
    public class NewsController : BaseController
    {
        public ActionResult List(int page = 1, int count = DefaultCount)
        {
            var list = Service.FindAll<News>();

            page = page < 1 ? 1 : page;
            count = count < 0 ? DefaultCount : count;
            var items = list.Skip((page - 1) * count).Take(count).ToList();
            var mapping = Mapper.Map<List<News>, List<NewsModel>>(items);
            ViewBag.Paged = new StaticPagedList<NewsModel>(mapping, page, count, list.Count);
            return View();
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View(new NewsModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(NewsModel model)
        {
            if (ModelState.IsValid)
            {
                Service.AddNews(model.Title, model.Category, model.Content);
                return RedirectToAction("List");
            }

            ModelState.AddModelError("", "所有字段都要填写。");
            return View(model);
        }

        [HttpGet]
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var news = Service.Find<News>(id.Value);
            if (news == null)
            {
                return HttpNotFound();
            }
            var model = Mapper.Map<News, NewsModel>(news);
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(NewsModel model)
        {
            if (ModelState.IsValid)
            {
                Service.UpdateNews(model.Id, model.Title, model.Category, model.Content);
                return RedirectToAction("List");
            }

            ModelState.AddModelError("", "所有字段都要填写。");
            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            Service.Delete<News>(id.Value);
            return Json(id);
        }

        [HttpGet]
        public ActionResult Banner()
        {
            var item = Service.GetBanner(1);
            if (item == null)
            {
                item = new Banner();
            }

            var model = Mapper.Map<Banner, BannerModel>(item);
            return View(model);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Banner(BannerModel model)
        {
            if (ModelState.IsValid)
            {
                Service.SetBanner(1, model.ImageId,string.Empty);
                return RedirectToAction("Banner");
            }

            ModelState.AddModelError("", "所有字段都要填写。");
            return View(model);
        }
    }
}
